package com.videoweb.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class DurationUtils {

    /**
     * 将七牛云返回的 duration 字符串（如 "29.791667"）转为毫秒（Long）
     */
    public static Long toMillisecond(String qiniuDuration) {
        if (qiniuDuration == null || qiniuDuration.isEmpty()) {
            return 0L;
        }
        // 使用 BigDecimal 精确处理小数
        BigDecimal seconds = new BigDecimal(qiniuDuration);
        BigDecimal milliseconds = seconds.multiply(BigDecimal.valueOf(1000));
        return milliseconds.setScale(0, RoundingMode.HALF_UP).longValueExact(); // 精确转换，如果溢出会抛异常
    }

    /**
     * 将毫秒转换为格式化的字符串，如 "00:29.792"
     */
    public static String formatToTime(Long millis) {
        if (millis == null || millis <= 0) {
            return "00:00.000";
        }

        long totalSeconds = millis / 1000;
        long minutes = totalSeconds / 60;
        long seconds = totalSeconds % 60;
        long ms = millis % 1000;

        return String.format("%02d:%02d.%03d", minutes, seconds, ms);
    }

    /**
     * 将毫秒转换为-->分秒，如 "00:29"  通用格式
     */
    public static String getTime(Long millis) {
        if (millis == null || millis <= 0) {
            return "00:00";
        }
        long totalSeconds = millis / 1000;
        long minutes = totalSeconds / 60;
        long seconds = totalSeconds % 60;

        return String.format("%02d:%02d", minutes, seconds);
    }

    /**
     * 获取秒数（保留三位小数），用于展示或计算
     */
    public static Double toSecondsDouble(Long millis) {
        if (millis == null) return 0.0;
        return millis / 1000.0;
    }

    /**
     * 获取分钟数（保留两位小数）
     */
    public static Double toMinutesDouble(Long millis) {
        if (millis == null) return 0.0;
        return millis / 60000.0;
    }

    /**
     * 获取小时数（保留两位小数）
     */
    public static Double toHoursDouble(Long millis) {
        if (millis == null) return 0.0;
        return millis / 3600000.0;
    }











}
